package org.dromara.tearoom.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.tearoom.domain.vo.TerOrderVo;
import org.dromara.tearoom.mapper.TerOrderMapper;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerUserSubscriptionBo;
import org.dromara.tearoom.domain.vo.TerUserSubscriptionVo;
import org.dromara.tearoom.domain.TerUserSubscription;
import org.dromara.tearoom.mapper.TerUserSubscriptionMapper;
import org.dromara.tearoom.service.ITerUserSubscriptionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户消息订阅Service业务层处理
 *
 * @author ckj
 * @date 2025-04-19
 */
@RequiredArgsConstructor
@Service
public class TerUserSubscriptionServiceImpl implements ITerUserSubscriptionService {

    private final TerUserSubscriptionMapper baseMapper;
    private final TerOrderMapper terOrderMapper;

    /**
     * 查询用户消息订阅
     *
     * @param id 主键
     * @return 用户消息订阅
     */
    @Override
    public TerUserSubscriptionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户消息订阅列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户消息订阅分页列表
     */
    @Override
    public TableDataInfo<TerUserSubscriptionVo> queryPageList(TerUserSubscriptionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerUserSubscription> lqw = buildQueryWrapper(bo);
        Page<TerUserSubscriptionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户消息订阅列表
     *
     * @param bo 查询条件
     * @return 用户消息订阅列表
     */
    @Override
    public List<TerUserSubscriptionVo> queryList(TerUserSubscriptionBo bo) {
        LambdaQueryWrapper<TerUserSubscription> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerUserSubscription> buildQueryWrapper(TerUserSubscriptionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TerUserSubscription> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerUserSubscription::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), TerUserSubscription::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), TerUserSubscription::getOpenId, bo.getOpenId());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplateId()), TerUserSubscription::getTemplateId, bo.getTemplateId());
        lqw.eq(bo.getSubscribed() != null, TerUserSubscription::getSubscribed, bo.getSubscribed());
        return lqw;
    }

    /**
     * 新增用户消息订阅
     *
     * @param bo 用户消息订阅
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerUserSubscriptionBo bo) {
        TerUserSubscription add = MapstructUtils.convert(bo, TerUserSubscription.class);
        TerUserSubscriptionVo subscriptionVo=validEntityBeforeSave(add);
        if(ObjectUtil.isNotEmpty(subscriptionVo)){
            throw new ServiceException("该订单已订阅成功!");
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户消息订阅
     *
     * @param bo 用户消息订阅
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerUserSubscriptionBo bo) {
        TerUserSubscription update = MapstructUtils.convert(bo, TerUserSubscription.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private TerUserSubscriptionVo validEntityBeforeSave(TerUserSubscription entity){
        return baseMapper.selectVoOne(new LambdaQueryWrapper<TerUserSubscription>().eq(TerUserSubscription::getOrderId,entity.getOrderId()));
    }

    /**
     * 校验并批量删除用户消息订阅信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 查询用户消息订阅
     *
     * @param orderId 订单ID
     * @return 用户消息订阅
     */
    @Override
    public TerUserSubscriptionVo queryVoByOrderId(String orderId) {
        TerUserSubscriptionVo subscriptionVo=baseMapper.selectVoOne(new LambdaQueryWrapper<TerUserSubscription>().eq(TerUserSubscription::getOrderId, orderId));
        if(ObjectUtil.isNotEmpty(subscriptionVo)){
            TerOrderVo orderVo=terOrderMapper.selectVoById(subscriptionVo.getOrderId());
            if(ObjectUtil.isNotEmpty(orderVo)){
                subscriptionVo.setRoomName(orderVo.getTearoomName());
                subscriptionVo.setRemark("客人已经退房请及时打扫房");
            }
        }
        return subscriptionVo;
    }
}
